import { _decorator, Button, Label, Node, UITransform } from 'cc';
import { BasePopUp } from '../../../frame/component/ui.pop-up';
import GameDC from '../../data/data.game.center';
import DataManager from '../../manager/data.manager';
import { Float } from '../page/float';
import type { Popup } from '../page/pop-up';
const { ccclass, property } = _decorator;

@ccclass('ppProp')
export class ppProp extends BasePopUp<typeof Popup, number> {
	protected default_return: null;

	private ui_container: UITransform;
	private lbl_desc: Label;

	protected initAfterOnLoad(){
		this.init();
	}

	private active_icon: Node;
	private type: 'stash'|'revoke'|'shuffle';

	public onShow(prop:'stash'|'revoke'|'shuffle'){
		this.type = prop;
		let {desc, name} = PropConfig[prop];
		this.lbl_desc.string = desc;

		['icon-prop-stash', 'icon-prop-revoke', 'icon-prop-shuffle'].forEach(n=>this.ui_container.node.getChildByName(n).active = n==name);

		this.active_icon = this.ui_container.node.getChildByName(name);

		BasePopUp.jelly_enter(this.node);
	}

	private init(){
		this.ui_container = this.node.getChildByName('container').getComponent(UITransform);
		this.lbl_desc = this.ui_container.node.getChildByName('lbl-desc').getComponent(Label);
		this.ui_container.node.children.forEach(n=>n.active = !n.name.startsWith('icon-prop-'));
		this.ui_container.node.getChildByName('lbl-diamond').getComponent(Label).string = '100';
	}

	private btn_lock: boolean = false;
	protected _click_event_: { [name: string]: (this: ppProp, button: Button) => void; } = {
		close(){
			if(this.btn_lock){
				return void 0;
			}
			this.btn_lock = true;
			this.exit(0);
		},
		async diamond(){
			if(this.btn_lock){
				return void 0;
			}
			this.btn_lock = true;

			let diamond = GameDC.User.diamond;
			if(!(diamond>=100)){
				await this.pop.Diamond();
				diamond = GameDC.User.diamond;
			}

			if(diamond>=100){
				DataManager.changeDiamond(-100);
				Float.addProp(this.type, this.active_icon);
				this.exit(1);
			}else{
				await Float.Hint('Not enough diamonds!');
				this.btn_lock = false;
			}
		}
	}

	private exit(count: number){
		BasePopUp.jelly_leave(this.node)
		.then(()=>{
			this.close(count);
			this.btn_lock = false;
		});
	}
}

const PropConfig = {
	stash: {
		desc: 'Remove 3 blocks',
		name: 'icon-prop-stash',
	},
	revoke: {
		desc: 'Recall one goods of your choice!',
		name: 'icon-prop-revoke',
	},
	shuffle: {
		desc: 'Rearrange all blocks',
		name: 'icon-prop-shuffle',
	},
}

